package com.xyz.practice.reflection;

import java.lang.reflect.Method;


public class ReflectionUtil {

  public static Object invokeMethod( Object bean, String methodName ) throws Exception {
    return invokeMethod( bean, methodName, null, null );
  }

  public static <T> Object invokeMethod( Object bean, String methodName, Object[] methodParameters ) throws Exception {
    return invokeMethod( bean, methodName, getClasses( methodParameters ), methodParameters );
  }

  public static <T> Object invokeMethod( Object bean, String methodName, Class<? extends Object>[] classes,
      Object[] methodParameters ) throws Exception {
    Method method = bean.getClass().getDeclaredMethod( methodName, classes );
    return method.invoke( bean, methodParameters );
  }

  public static Class<? extends Object>[] getClasses( Object... objects ) {
    if ( objects != null && objects.length > 0 ) {
      Class<? extends Object>[] classes = new Class<?>[objects.length];
      for ( int i = 0; i < objects.length; i++ ) {
        classes[i] = objects[i].getClass();
      }
      return classes;
    }
    return null;
  }
}
